/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package neuralnetwork;

import java.io.FileNotFoundException;
import java.io.IOException;
import org.neuroph.core.learning.DataSet;
import org.neuroph.util.TrainingSetImport;
import org.neuroph.util.TransferFunctionType;

/**
 *
 * @author Rosana
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        // setup
        String dataSetFileName = "trainingSet.txt";

        int inputsCount = 41;
        int outputsCount = 1;

        // create training set (It's used to traning and test the ANN)
        DataSet dataSet = null;
        DataSet trainingSet = null;
        DataSet testingSet = null;
        try {
            dataSet = TrainingSetImport.importFromFile(dataSetFileName, inputsCount, outputsCount, ",");
        } catch (FileNotFoundException ex) {
            System.out.println("TrainingSet file not found!");
        } catch (IOException | NumberFormatException ex) {
            System.out.println("Error reading file or bad number format!");
            System.out.println("Using COMA ',' separator?");
        }

        // normalize the data (between 0 and 1)
        dataSet.normalize();
        
        // here 20% is used to training ANN and 80% from dataset is used to test
       DataSet[] assets = dataSet.createTrainingAndTestSubsets(20, 80);
       trainingSet = assets[0];
       testingSet = assets[1];

        // neural net
        NeuralNetwork neuralNet = new NeuralNetwork(
                inputsCount,// neurons in input layer
                30,// neurons in hidden layer
                outputsCount, // neurons in output layer
                TransferFunctionType.SIGMOID); // transfer function

        // neural net setup
        neuralNet.setLearningRate(0.8188976377952756);
        neuralNet.setMomentum(0.7165354330708661);
        neuralNet.setMaxError(0.00946);

        // training Step
        neuralNet.learn(trainingSet);

        // testing result
        double totalError = 100 * (neuralNet.test(testingSet) / (1.0*testingSet.size()));
        System.out.println("Taxa de Acerto: " + (100.0-totalError) + "%");
    }

}
